package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.SysUserEntity;
import com.aaa.entity.User;
import com.aaa.entity.Pager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public Map login(User user) {
        String sql = "select * from sys_user where login_name=? and password=? ";
        Object[] params = new Object[]{user.getLogin_name(),user.getPassword()};
        List<Map> mapList = this.executeQuery(sql, params);
        return mapList.size() > 0 ? mapList.get(0) : null;
    }

    @Override
    public List queryAll() {
        String sql="select * from sys_user";
        return  this.executeQuery(sql);
    }

    @Override
    public int update(SysUserEntity user) {
        String sql="update sys_user set dept_id=?,login_name=?,user_name=?,password=?,email=?,phonenumber=?,sex=?,status=?,remark=? where user_id=?";
        return this.executeUpdate(sql,user.getDeptId(),user.getLoginName(),user.getUserName(),user.getPassword(),user.getEmail(),user.getPhonenumber(),user.getSex(),user.getStatus(),user.getRemark(),user.getUserId());
    }

    @Override
    public int delete(int user_id) {
        String sql="update sys_user set del_flag=1 where user_id=?";
        return this.executeUpdate(sql,user_id);
    }

    @Override
    public int add(SysUserEntity user) {
        String sql="insert into sys_user(dept_id,login_name,user_name,password,email,phonenumber,sex,status,remark) values(?,?,?,?,?,?,?,?,?)";
        return this.executeInsert(sql,user.getDeptId(),user.getLoginName(),user.getUserName(),user.getPassword(),user.getEmail(),user.getPhonenumber(),user.getSex(),user.getStatus(),user.getRemark());

    }

    @Override
    public Pager query(int curPage, int pageSize, Map condition) {
        String sql="select * from sys_user where 1=1 and del_flag=0 ";
        StringBuffer buffer=new StringBuffer(sql);
        List params=new ArrayList();
        if (condition!=null)
        {
            if (condition.containsKey("user_name")&&condition.get("user_name").toString().length()>0)
            {
                buffer.append("and user_name like ?");
                params.add("%"+condition.get("user_name")+"%");
            }
            if (condition.containsKey("sex")&&condition.get("sex").toString().length()>0)
            {
                buffer.append("and sex like ?");
                params.add("%"+condition.get("sex")+"%");
            }
            if (condition.containsKey("remark")&&condition.get("remark").toString().length()>0)
            {
                buffer.append("and remark like ?");
                params.add("%"+condition.get("remark")+"%");
            }
            if (condition.containsKey("status")&&condition.get("status").toString().length()>0)
            {
                buffer.append("and status like ?");
                params.add("%"+condition.get("status")+"%");
            }
        }
        sql="select count(*) c from ("+buffer+") user";
        Map map=this.executeQuery(sql,params.toArray()).get(0);
        Long  count= (Long) map.get("c");

        buffer.append("limit ?,?");
        int startindex=(curPage-1)*pageSize;
        params.add(startindex);
        params.add(pageSize);
        List list=this.executeQuery(buffer.toString(),params.toArray());
        Pager pager=new Pager(curPage,pageSize,list,count.intValue());
        return pager;
    }

    @Override
    public Map queryid(int user_id) {
        String sql="select * from sys_user where user_id=?";
        List<Map> list=this.executeQuery(sql,user_id);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public List selectloginname(String login_name) {
        String sql="select * from sys_user where login_name=? and del_flag=0";
        return executeQuery(sql,login_name);

    }
}
